/**
 *    Copyright 2009-2020 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.aapl.script.creator;

import org.apache.ibatis.aapl.annotation.TableWatcher;
import org.apache.ibatis.aapl.util.Clause;
import org.apache.ibatis.aapl.util.ClauseUtil;
import org.apache.ibatis.aapl.util.NameUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class UpdateAllSqlCreator implements SqlCreator {
  @Override
  public String createSql(String script, Method method, TableWatcher watcher) {
    Class<?>[] parameterTypes = method.getParameterTypes();
    if (parameterTypes.length < 1) {
      return script;
    }
    Class<?> clazz = parameterTypes[0];
    if (Collection.class.isAssignableFrom(clazz)) {
      return script;
    }
    if (Map.class.isAssignableFrom(clazz)) {
      return script;
    }
    String s = doCreateSql(script, clazz, watcher);
    return s == null ? script : s;
  }

  protected String doCreateSql(String script, Class<?> clazz, TableWatcher watcher) {
    Field[] declaredFields = clazz.getDeclaredFields();
    List<String> fields = new LinkedList<>();
    for (int i = 0; i < declaredFields.length; i++) {
      if (Modifier.isStatic(declaredFields[i].getModifiers())) {
        continue;
      }
      fields.add(declaredFields[i].getName());
    }

    Clause clause = ClauseUtil.parse(script);
    return buildSql(fields, watcher, clause);
  }

  protected String buildSql(List<String> fields, TableWatcher watcher, Clause clause) {

    StringBuilder sb = getStringBuilder(watcher);
    List<String> excepts = clause.except();

    fields.removeAll(excepts);
    while (fields.size() > 0) {
      String str = fields.get(0);
      if ("id".equals(str)) {
        fields.remove(0);
        continue;
      }
      sb.append(NameUtil.camel2Underline(str));
      sb.append(" = ");
      sb.append("#{");
      sb.append(str);
      sb.append("}");

      if (fields.size() > 1) {
        sb.append(",");
      }
      fields.remove(0);
    }
    sb.append(" where id = #{id} ");
    return sb.toString();
  }

  protected StringBuilder getStringBuilder(TableWatcher watcher) {
    StringBuilder sb = new StringBuilder();
    sb.append(" update ");
    sb.append(watcher.value());
    sb.append(" set ");
    return sb;
  }
}
